using System.Collections.Generic;
using UnityEngine;

public class PoisonItem : Npc
{
    [Header("coll")]
    public GameObject collObj;
    [Header("毒雾")]
    public GameObject poisonFog;
    [Header("伤害时间")]
    public float damageTimer = 1f;
    public float damageStopTimer = 1f;
    [Header("持续时间")]
    public float durationTimer = 10;
    public float durationStopTimer = 10;

    private void Start()
    {
        collObj = transform.Find("CheckPosOne").gameObject;

        //隐藏
        collObj.GetComponent<Collider2D>().enabled = false;
    }
    private void Update()
    {
        DamageAll();
        CalculateDurationTimer();
    }
    private void OnMouseDown()
    {
        SceneItemManager.instance.clickEnemyRight = true;
        collObj.GetComponent<Collider2D>().enabled = true;
        SceneItemManager.instance.SetClickEnemy(gameObject);
    }
    //打开UI
    public void OpenUI()
    {
        if (poisonFog.activeSelf) return;
        //打开破坏提示
        PanelManager.Open<PoisonTip>(this);
        //关闭碰撞enable
        collObj.GetComponent<Collider2D>().enabled = false;
    }
    //伤害
    public void DamageAll()
    {
        if(poisonFog.activeSelf)
        {
            //计算时间
            damageTimer-=Time.deltaTime;
            if(damageTimer<=0)
            {
                damageTimer=damageStopTimer;
                //进行伤害
                List<GameObject> list = SceneItemManager.instance.enemies;
                int damage = Mathf.RoundToInt(GameManager.Instance.player.currHp*0.05f);
                damage=damage<=0 ? 1 : damage;
                foreach(var i in list)
                {
                    if(i.activeSelf)
                    {
                        i.GetComponent<Enemy>().BeHurt(damage, false);
                    }
                }
                if(!TurnManager.instance.SeekTurn("猪"))
                {
                    GameManager.Instance.player.BeHurt(damage);
                    if(GameManager.Instance.player.currHp<=0)
                    {
                        poisonFog.SetActive(false);
                    }
                }
            }
        }
    }
    //播放结束
    public void OverPlay()
    {
        if (GetComponent<SpriteRenderer>().enabled==false) return;
        //影藏自己
        GetComponent<SpriteRenderer>().enabled=false;
        //打开毒物
        poisonFog.SetActive(true);
        //可行走
        SceneItemManager.instance.SetRun(transform);
        //已经没有了
        SceneItemManager.instance.SetNpcFanish(sayData.myName);
        //保存一下玩家数据
        //保存一下数据
        SaveManager.instance.SaveAllData();

        AchiManager.instance.UnLockAchi("毒雾");
    }
    //计算持续时间
    public void CalculateDurationTimer()
    {
        if(poisonFog.activeSelf)
        {
            durationTimer-=Time.deltaTime;
            if(durationTimer<=0)
            {
                durationTimer=durationStopTimer;
                //关闭毒物
                poisonFog.SetActive(false);
            }
        }
    }
}
